package com.lagou.utils;

import com.lagou.annotation.QjcAutowired;
import com.lagou.annotation.QjcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@QjcService
public class TransactionManager {

    @QjcAutowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    // 开启事务
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConnection().setAutoCommit(false);
    }

    // 提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConnection().commit();
    }

    // 回滚事务
    public void rollBack() throws SQLException {
        connectionUtils.getCurrentThreadConnection().rollback();
    }
}
